package com.hrm.utils;

import java.awt.Dimension;
import java.io.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

public class PPTtoHtml {

    /**
     * @param inputPath H:/img2/
     * @param fileName  格式：文件名+后缀
     */
    public static void pptToHtml(String inputPath, String fileName, String userId) {
        // inputPath
        //word路径
        String inputFile = inputPath + fileName;
        //文件夹名
        String folderName = fileName.substring(0, fileName.lastIndexOf("."));
        List<String> imgList = new ArrayList<>();
        File file = new File(inputFile);

        File folder = new File(inputPath + folderName);
        try {
            folder.mkdirs();
            FileInputStream is = new FileInputStream(file);
            SlideShow ppt = new SlideShow(is);
            is.close();
            Dimension pgsize = ppt.getPageSize();
            org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
            for (int i = 0; i < slide.length; i++) {
                TextRun[] truns = slide[i].getTextRuns();
                for (int k = 0; k < truns.length; k++) {
                    RichTextRun[] rtruns = truns[k].getRichTextRuns();
                    for (int l = 0; l < rtruns.length; l++) {
                        rtruns[l].setFontIndex(1);
                        rtruns[l].setFontName("宋体");
                    }
                }
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.BLUE);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
                slide[i].draw(graphics);
                // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
                String imgName = folderName +  "/pict_" + (i + 1) + ".png";
                FileOutputStream out = new FileOutputStream(inputPath + imgName);
                javax.imageio.ImageIO.write(img, "png", out);
                out.close();
                imgList.add(imgName);
            }
            // 将多张图片拼接转成html，参数：路径，不带后缀的文件名，图片路径的集合
            createHtml(inputPath, folderName, imgList, userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static int createHtml(String inputPath, String fileName, List<String> imgList, String userId) {
        int rv = 0;
        StringBuilder sb = new StringBuilder("<!doctype html><html><head><meta charset='utf-8'><title>" + fileName + "</title></head><body>");
        if (imgList != null && !imgList.isEmpty()) {
            String str = "";
            if (userId != null && !userId.equals("")) {
                str = "&userId=" + userId;
            }
            for (String img : imgList) {
                sb.append("<img src='./getfile?path=" + img + str + "' /><br>");
            }
        }
        sb.append("</body></html>");
        try {
            File file = new File(inputPath + fileName + ".html");
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bufferedWriter.write(sb.toString());
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            return rv;
        }
        return 1;
    }
}